import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'TreeHoleList.dart';

String myTitle;
String myContent;
class BuildTreeHolePage extends StatelessWidget {
  final String token;
  BuildTreeHolePage(this.token);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        resizeToAvoidBottomPadding:false,
        appBar: AppBar(
          title: Container(
            child: Text(
              '创建树洞',
              style: TextStyle(
                color: Colors.black,
              ),
              textAlign: TextAlign.center,
            ),
          ),
          centerTitle:true,
          backgroundColor: Colors.white,
          leading: IconButton(
            icon:Icon(Icons.arrow_back,color: Colors.black),
            onPressed: () {Navigator.pop(context);},
          ),
        ),
        body: Container(
          height: double.infinity,
          child: Column(
            children: [
              SizedBox(height: 10,),
              ListTile(
                title: TextField(
                  decoration: InputDecoration(
                      contentPadding: EdgeInsets.all(10),
                      hintText: '标题',
                      filled: true,
                      fillColor: Colors.transparent,
                      focusedBorder: OutlineInputBorder(
                        borderSide: BorderSide(
                          color: Colors.black,
                        ),
                      ),
                      border: OutlineInputBorder(
                        borderSide: BorderSide(
                          color: Colors.black,
                        ),
                      )
                  ),
                  onChanged: (value){
                    myTitle=value;
                  },
                ),
                subtitle: Container(
                  margin: EdgeInsets.only(top: 5),
                  child: TextField(
                    decoration: InputDecoration(
                        contentPadding: EdgeInsets.all(10),
                        hintText: '内容',
                        filled: true,
                        fillColor: Colors.transparent,
                        focusedBorder: OutlineInputBorder(
                            borderSide: BorderSide(
                              color: Colors.black,
                            )
                        ),
                        border: OutlineInputBorder(
                          borderSide: BorderSide(
                            color: Colors.black,
                          ),
                        ),
                      isCollapsed: true,
                    ),
                    maxLines: 12,
                    onChanged: (value){
                      myContent=value;
                      print(myContent);
                    },
                  ),
                ),
              ),
              SizedBox(height: 70),
              RaisedButton(
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10)
                  ),
                  child: Text('创建树洞'),
                  textColor: Colors.black,
                  color: Colors.green[100],
                  padding: EdgeInsets.all(10),
                  onPressed: () async {
                    String result;
                    if(myContent==null){
                      result='内容不能为空';
                    }
                    else {
                      result=await TreeHoleList.addArticle(this.token, myTitle, myContent);
                      if(result=='创建成功'){
                        Navigator.pop(context);//返回后TreeHolePage无新建树洞(无伤大雅)
                      }
                    }
                    Fluttertoast.showToast(
                        msg: result,
                        backgroundColor: Colors.amber[50],
                        toastLength: Toast.LENGTH_SHORT,
                        gravity: ToastGravity.BOTTOM,
                        timeInSecForIosWeb: 1,
                        textColor: Colors.black,
                        fontSize: 16.0
                    );
                  }
              ),
            ],
          ),
          decoration: BoxDecoration(
              image: new DecorationImage(
                  fit: BoxFit.cover,
                  image: new AssetImage('images/treeHole.jpg'))),
        )
    );
  }
}
